package com.iris.live.services.services.impl;

import com.google.common.base.Strings;
import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.common.reconsitution.BeanUtils;
import com.iris.live.services.data.models.*;
import com.iris.live.services.data.repositories.*;
import com.iris.live.services.interceptors.SessionInterceptor;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.CustomerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 此 类 里去除了 事物，不会有事物控制；
 * Created by lusong on 2016/10/31.
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    private static Logger logger = LoggerFactory.getLogger(CustomerInfoServiceImpl.class);

    @Autowired
    private 报价单表Repository repository报价单表;

    @Autowired
    private 报价订单表Repository repository报价订单表;

    @Autowired
    private 报价交车表Repository repository报价交车表;

    @Autowired
    private Lsh导入名单Repository lsh导入名单Repository;

    @Autowired
    private 顾客记录汇总Repository repository顾客记录汇总;

    @Autowired
    private 顾客订单记录Repository repository顾客订单记录;

    @Autowired
    private 顾客进店记录表Repository repository顾客进店记录表;

    @Autowired
    private 用户Repository 用户Repository;

    @Autowired
    private ApiForLiveRepository apiForLiveRepository;

    @Autowired
    private CustomerInfoRepository customerInfoRepository;

    @Autowired
    private 顾客试驾记录Repository 顾客试驾记录Repository;

    @Autowired
    private TIlogTransferLogRepository tIlogTransferLogRepository;
    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Autowired
    private ILogInfoRepository iLogInfoRepository;

    /**
     * 批量新增导入名单表
     *  lusong
     * @param list
     * @return 成功保存成功条数
     */
    @Override
    public int insertLeadingList(List<Lsh导入名单Model> list) {
        int insertNum = 0;
        Lsh导入名单Model save = null;
        for(Lsh导入名单Model lsh导入名单Model:list){
            try{
                lsh导入名单Model.set更新时间(new Timestamp(new Date().getTime()));
                save = lsh导入名单Repository.save(lsh导入名单Model);
                if(save != null){
                    insertNum++;
                }
            }catch(Exception e){
                logger.error("系统异常。", e);
            }
        }
        return insertNum;
    }



    /**
     * 批量新增顾客记录汇总
     *  lusong
     * @param list
     * @return 成功保存条数, 已存在数据条数
     */
    @Override
    public String insertCustomerCollect(List<顾客记录汇总Model> list) {
        int insertNum = 0;
        int existNum = 0;
        顾客记录汇总Model getModel = null;
        for (顾客记录汇总Model model顾客记录汇总 : list) {
            try{
                getModel = repository顾客记录汇总.findOneCus(model顾客记录汇总.get电话号码(), model顾客记录汇总.get许可());
                if (getModel != null) {
                    existNum++;
                } else {
                    model顾客记录汇总.set更新时间(new Timestamp(new Date().getTime()));
                    model顾客记录汇总 = repository顾客记录汇总.save(model顾客记录汇总);
                    if(model顾客记录汇总 != null){
                        insertNum++;
                    }
                }
            }catch(Exception e){
                logger.error("系统异常。", e);
            }
        }
        StringBuffer returnValue = new StringBuffer();
        returnValue.append("保存数据条数:");
        returnValue.append(insertNum);
        returnValue.append("条,存在数据条数:");
        returnValue.append(existNum);
        returnValue.append("条");
        return returnValue.toString();
    }

    /**
     * 批量新增顾客订单记录
     *  lusong
     * @param list
     * @return 成功保存条数 不存在数据条数
     */
    @Override
    public String insertCustomerOrders(List<GetCustomerOrdersRequest> list) {
        int insertNum = 0;
        int notExistNum = 0;
        顾客记录汇总Model model顾客记录汇总 = null;
        顾客订单记录Model model顾客订单记录 = null;
        for (GetCustomerOrdersRequest getCustomerOrdersRequest : list) {
            model顾客记录汇总 = repository顾客记录汇总.findOneCus(getCustomerOrdersRequest.电话号码, getCustomerOrdersRequest.许可);
            try {
                if (model顾客记录汇总 != null) {
                    model顾客订单记录 = new 顾客订单记录Model();
                    model顾客订单记录.set顾客编号(model顾客记录汇总.get顾客编号());
                    model顾客订单记录.set许可(getCustomerOrdersRequest.许可);
                    //判断 传入参数  车型  是否为 空或null：是 返回null；否  返回 转换为Integer类型的值；
                    Integer carType = getCustomerOrdersRequest.车型 == null || "".equals(getCustomerOrdersRequest.车型) ? null : Integer.parseInt(getCustomerOrdersRequest.车型);
                    model顾客订单记录.set车型(carType);
                    model顾客订单记录.set销售顾问(getCustomerOrdersRequest.销售顾问);
                    //判断 传入参数  订单日期  是否为 空或null：是 返回null；否  返回 转换为Date类型的值；
                    Date orderDate = getCustomerOrdersRequest.订单日期 == null || "".equals(getCustomerOrdersRequest.订单日期) ? null : new SimpleDateFormat("yyyy-MM-dd").parse(getCustomerOrdersRequest.订单日期);
                    model顾客订单记录.set订单日期(orderDate);
                    //判断 传入参数  交车日期  是否为 空或null：是 返回null；否  返回 转换为Date类型的值；
                    Date getCarDate = getCustomerOrdersRequest.交车日期 == null || "".equals(getCustomerOrdersRequest.交车日期) ? null : new SimpleDateFormat("yyyy-MM-dd").parse(getCustomerOrdersRequest.交车日期);
                    model顾客订单记录.set交车日期(getCarDate);
                    model顾客订单记录.set状态(getCustomerOrdersRequest.状态);
                    model顾客订单记录.set渠道(getCustomerOrdersRequest.渠道);
                    //判断 传入参数  订单类型  是否为 空或null：是 返回null；否  返回 转换为Byte类型的值；
                    Byte orderType = getCustomerOrdersRequest.订单类型 == null || "".equals(getCustomerOrdersRequest.订单类型) ? null : Byte.decode(getCustomerOrdersRequest.订单类型);
                    model顾客订单记录.set订单类型(orderType);
                    //判断 传入参数  再购进店日期  是否为 空或null：是 返回null；否  返回 转换为Date类型的值；
                    Date againGoShopDate = getCustomerOrdersRequest.再购进店日期 == null || "".equals(getCustomerOrdersRequest.再购进店日期) ? null : new SimpleDateFormat("yyyy-MM-dd").parse(getCustomerOrdersRequest.再购进店日期);
                    model顾客订单记录.set再购进店日期(againGoShopDate);
                    model顾客订单记录.set更新账号(getCustomerOrdersRequest.更新账号);
                    model顾客订单记录.set更新时间(new Timestamp(new Date().getTime()));
                    model顾客订单记录 = repository顾客订单记录.save(model顾客订单记录);
                    if(model顾客订单记录 != null){
                        insertNum++;
                    }
                } else {
                    notExistNum++;
                }
            }catch(Exception e){
                logger.error("系统异常。", e);
            }
        }
        StringBuffer returnValue = new StringBuffer();
        returnValue.append("保存数据条数:");
        returnValue.append(insertNum);
        returnValue.append("条,不存在数据条数:");
        returnValue.append(notExistNum);
        returnValue.append("条");
        return returnValue.toString();
    }



    /**
     * 批量新增顾客进店记录
     *  lusong
     * @param list
     * @return 成功保存条数
     */
    @Override
    public String insertCustomerShop(HttpServletRequest request,List<顾客进店记录表Model> list) {
        int insertNum = 0;
        int notExistNum = 0;
        String permit = SessionInterceptor.getLicense(request);
        String username = SessionInterceptor.getUsername(request);
        顾客记录汇总Model model顾客汇总记录 = null;
        for (顾客进店记录表Model model顾客进店记录表:list) {
            try{
                model顾客汇总记录 = repository顾客记录汇总.findCusByCustomerID(model顾客进店记录表.get顾客编号());
                if(model顾客汇总记录 != null){
                    model顾客进店记录表.set许可(permit);
                    model顾客进店记录表.set更新账号(username);
                    model顾客进店记录表.set更新时间(new Timestamp(new Date().getTime()));
                    model顾客进店记录表 = repository顾客进店记录表.save(model顾客进店记录表);
                    if (model顾客进店记录表 != null){
                        insertNum++;
                    }
                }else{
                    notExistNum++;
                }
            }catch(Exception e){
                logger.error("系统异常。", e);
            }
        }
        StringBuffer returnValue = new StringBuffer();
        returnValue.append("保存数据条数:");
        returnValue.append(insertNum);
        returnValue.append("条，不存在数据条数:");
        returnValue.append(notExistNum);
        returnValue.append("条");
        return returnValue.toString();
    }

    @Override
    public ResultData hasCustomers(String account) {
        ResultData result = new ResultData();
        List<顾客记录汇总Model> customers = repository顾客记录汇总.findCustomersByAccount(account);
        if(customers != null && !customers.isEmpty()){
            result.setResult("true");
        }else{

        }
        return result;
    }

    @Override
    public List getCustomerInfoes(String permit, String scCode) {
        return apiForLiveRepository.getClueAboutSC(permit, scCode);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultData changeCustomers(String ownerAccount, String customerIds, String assignAccounts) {
        if(logger.isDebugEnabled()){
            logger.debug("ownerAccount={}, customerIds={}, assignAccounts={}", ownerAccount, customerIds, assignAccounts);
        }
        ResultData rd = new ResultData();
        if(StringUtils.isNullOrBlank(ownerAccount) || StringUtils.isNullOrBlank(customerIds) || StringUtils.isNullOrBlank(assignAccounts)){
            logger.warn("所有参数都不能为空。");
            rd.setResult("false");
            rd.setMessage("调用接口参数异常。");
            return rd;
        }


        String[] arrCustomerIds = customerIds.split(","), arrAssignAccounts = assignAccounts.split(",");//arrCustomerIds:待转客户ids......arrAssignAccounts:在职销售顾问s
        int customerLength = arrCustomerIds.length, assignAccountsLength = arrAssignAccounts.length;
        顾客记录汇总Model model = null;
        Lsh导入名单Model lsh导入名单Model = null;
        String customer = null;
        String customerId = null;
        String status = null;

        StringBuffer errorAccounts = new StringBuffer();
        for(String account : arrAssignAccounts){ //检查指定的用户ID合法性
            if(用户Repository.getOne(account) == null){
                if(errorAccounts.length() > 0){
                    errorAccounts.append(",");
                }
                errorAccounts.append(account);
            }
        }
        String cIds[]=new String[customerLength];
            StringBuffer missedCustomers = new StringBuffer(), unmatchedCustomers = new StringBuffer();
            for(int index = 0; index < customerLength; index++){ //将选定的客户分配给销售顾问
                customer = arrCustomerIds[index];
                if (!StringUtils.isNullOrBlank(customer)) {
                    String[] arrCustomer = customer.split("-");
                    if (arrCustomer.length == 2) {
                        status = arrCustomer[0];
                        customerId = arrCustomer[1];
                        cIds[index]=customerId;
                    }
                }
                if ("1".equals(status)) {
                    //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                    if (logger.isDebugEnabled()) {
                        logger.debug("顾客编号:" + customerId + ",查询顾客记录汇总信息");
                    }
                    model = repository顾客记录汇总.findOne(Integer.valueOf(customerId));
                    /**
                     *  检测订单状态是否可以转换
                     */
                    //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                    if (logger.isDebugEnabled()) {
                        logger.debug("顾客编号:" + customerId + ",查询最大报价编号");
                    }
                    List bjdMaxCheck= customerInfoRepository.getMaxQuotationNo(customerId,"", "");
                    if(bjdMaxCheck!=null&&bjdMaxCheck.size()>0){
                        Map bjdMap= (Map) bjdMaxCheck.get(0);
                        Object bjNo=bjdMap.get("订单编号");
                        String cName=model.get顾客姓名();
                        if(bjNo!=null){
                            //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                            if (logger.isDebugEnabled()) {
                                logger.debug("订单编号:" + bjNo + ",查询报价订单表信息");
                            }
                            String orderState=  repository报价订单表.selectOrderState(Integer.parseInt(bjNo+""));
                            List sl=new LinkedList();
                            sl.add("合同O");sl.add("意向订单");sl.add("订单O");sl.add("退订");sl.add("开票O");sl.add("退票O");

                            if(orderState!=null&&sl.contains(orderState)){
                                rd.setResult("false");
                                rd.setMessage(cName+"线索所属销售顾问提交的审批流程未完成，线索无法转换。");
                                return rd;
                            }
                        }
                    }

                    if (model == null) { //检查客户合法性
                        if (missedCustomers.length() > 0) {
                            missedCustomers.append(",");
                        } else {
                            unmatchedCustomers.append("顾客编号为:");
                        }
                        missedCustomers.append(customerId);
                        continue;
                    }

                    //数据库中存在销售顾问 的 账户 带空格导致批量转换时校验异常，现添加trim()方法，去除两边空格比较
                    if (!ownerAccount.equals(model.get所属销售顾问()) && !ownerAccount.equals(model.get所属销售顾问().trim())) { //检查客户原先所属销售顾问的合法性
                        if (unmatchedCustomers.length() > 0) {
                            unmatchedCustomers.append(",");
                        } else {
                            unmatchedCustomers.append("顾客:");
                        }
                        unmatchedCustomers.append(model.get顾客姓名());
                        continue;
                    }
                    model.set所属销售顾问(arrAssignAccounts[index % assignAccountsLength]);
                    //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                    if (logger.isDebugEnabled()) {
                        logger.debug("顾客编号:" + model.get顾客编号() + ",修改顾客记录汇总信息");
                    }
                    repository顾客记录汇总.save(model);

                    /**
                     *   转移顾客绑定信息
                     */
                    //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                    if (logger.isDebugEnabled()) {
                        logger.debug("顾客编号:" + customerId + ",查询最大报价编号， 是否还原点 = 是，是否驳回 = 否");
                    }
                    List bjdMax= customerInfoRepository.getMaxQuotationNo(customerId,"是", "否");
                    String csbjdh=null;
                    if(bjdMax!=null&&bjdMax.size()>0){
                        Map bjdMaxMap= (Map) bjdMax.get(0);
                        csbjdh= bjdMaxMap.get("初始报价单号")+"";
                        String xsgw = arrAssignAccounts[index % assignAccountsLength];//销售顾问
                        for (int i = 0; i <bjdMax.size(); i++) {
                            Map bjdMap= (Map) bjdMax.get(i);
                            Object baojiaNo=bjdMap.get("报价编号");
                            if(!xsgw.equals("null")&&!csbjdh.equals("null")&& csbjdh!=null){
                                //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                                if (logger.isDebugEnabled()) {
                                    logger.debug("初始报价单号:" + csbjdh + ",通过初始报价单号，修改报价单表信息");
                                }
                                int c= repository报价单表.updateBjdOnXsgw(xsgw,csbjdh);//SET 销售顾问=? WHERE 初始报价单号=？
                                logger.info("更新报价单表："+c);
                            }
//                        Map bjdMap= (Map) bjdMax.get(0);
                            List list=null;
                            if (baojiaNo!=null ){
                                list= customerInfoRepository.getUnpaidCar(Integer.parseInt(baojiaNo.toString()));
                            }else{
                                logger.warn("根据最大顾客编号未找到关联数据:----",baojiaNo);
                            }
                            if(list!=null&&list.size()>0){
                                Map map= (Map) list.get(0);
                                String kcbh= map.get("库存编号")+"";
                                String gkbh= map.get("顾客编号")+"";
                                if(!xsgw.equals("null")&&!gkbh.equals("null")&&!kcbh.equals("null")) {
                                    if(logger.isInfoEnabled()) {
                                        logger.info("销售顾问:" + xsgw + ",顾客编号:" + gkbh + ",库存编号:" + kcbh);
                                    }
                                    int c2 = repository顾客订单记录.updateGkddInfo(xsgw, Integer.parseInt(gkbh), Integer.parseInt(kcbh));
                                    if(logger.isInfoEnabled()) {
                                        logger.info("更新顾客订单记录表：" + c2);
                                    }
                                }
                            }
                        }
                    }else{
                        logger.warn("没有找到客户编号的最大报价编号");
                    }
                } else if ("2".equals(status)) {
                    lsh导入名单Model = lsh导入名单Repository.findOne(Integer.valueOf(customerId));
                    if (lsh导入名单Model == null) { //检查客户合法性
                        if (missedCustomers.length() > 0) {
                            missedCustomers.append(",");
                        } else {
                            unmatchedCustomers.append("导入编号为:");
                        }
                        missedCustomers.append(customerId);
                        continue;
                    }

                    if (!ownerAccount.equals(lsh导入名单Model.get所属销售顾问())) { //检查客户原先所属销售顾问的合法性
                        if (unmatchedCustomers.length() > 0) {
                            unmatchedCustomers.append(",");
                        } else {
                            unmatchedCustomers.append("顾客:");
                        }
                        unmatchedCustomers.append(lsh导入名单Model.get顾客姓名());
                        continue;
                    }

                    lsh导入名单Model.set所属销售顾问(arrAssignAccounts[index % assignAccountsLength]);
                    //bug 5610 未查询到死锁原因，所以添加debug日志，用以记录操作信息
                    if (logger.isDebugEnabled()) {
                        logger.debug("导入编号:" + lsh导入名单Model.get导入编号() + ",通过导入编号，修改导入名单表信息");
                    }
                    lsh导入名单Repository.save(lsh导入名单Model);
                }
            }

            if(missedCustomers.length() > 0){
                missedCustomers.append("不存在。");
            }

            if(unmatchedCustomers.length() > 0){
                unmatchedCustomers.append("不属于当前用户:").append(ownerAccount);
            }

            if(errorAccounts.length() > 0 || unmatchedCustomers.length() > 0 || missedCustomers.length() > 0){
                rd.setMessage(errorAccounts.append("\n").append(unmatchedCustomers.toString()).append("\n").append(missedCustomers.toString()).toString());
            }

        return rd;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData dataMoveLeaningInListOverWrite(String permit, List<Map> list) throws Exception{
        ResultData resultData = new ResultData("false", null, null);
        StringBuffer message = new StringBuffer();

        //校验经销商许可是否注册
        Lsh经销商表Model lsh经销商表Model = lsh经销商表Repository.getLshDistributorByPermit(permit);
        if (lsh经销商表Model == null) {
            resultData.setMessage("经销商不存在");
            return resultData;
        }

        if (list != null && !list.isEmpty()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            List<Lsh导入名单Model> lsh导入名单Models = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();

            //获取所有对照车型编号
            List listCarIds = customerInfoRepository.getCarId();
            Map<String, String> mapCarId = getCarIdMap(listCarIds);

            //获取该许可的所有有效线索
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -2);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            java.sql.Date dateLeadingIn = new java.sql.Date(calendar.getTime().getTime());
            List listLeadingIn = customerInfoRepository.getLshLeadingIn(permit, dateLeadingIn);
            Map<String, String> mapLeadingIn = getLeadingInMap(listLeadingIn);

            Map<String, String> mapTel = new HashMap<>();

            for (Map mapBean : list) {
                Object o = BeanUtils.mapToObject(mapBean, GetLeaningInList.class);
                GetLeaningInList getLeaningInList = (GetLeaningInList) o;
                Lsh导入名单Model lsh导入名单Model = new Lsh导入名单Model();
                lsh导入名单Model.set许可(permit);
                lsh导入名单Model.set顾客姓名(getLeaningInList.顾客姓名);
                //校验电话号码是否为空
                if (!Strings.isNullOrEmpty(getLeaningInList.电话号码)) {
                    if (mapTel.get(getLeaningInList.电话号码) != null && !"".equals(mapTel.get(getLeaningInList.电话号码))) {
                        message.append("客户姓名：" + getLeaningInList.顾客姓名 + "，电话号码:" + getLeaningInList.电话号码 +",有重复电话号码；\n");
                        continue;
                    }
                    mapTel.put(getLeaningInList.电话号码,getLeaningInList.电话号码);
                    lsh导入名单Model.set电话号码(getLeaningInList.电话号码);
                } else {
                    message.append("客户姓名：" + getLeaningInList.顾客姓名 + "，电话号码为空；\n");
                    continue;
                }
                //校验lsh导入名单表中该电话号码是否已有有效线索
                if (mapLeadingIn.get(getLeaningInList.电话号码) != null) {
                    message.append("客户姓名：" + getLeaningInList.顾客姓名 + "，电话号码:" + getLeaningInList.电话号码 + ",已存在有效线索；\n");
                    continue;
                }

                lsh导入名单Model.set渠道(getLeaningInList.名单类型);
                //通过ilog_car获取对应车型的车型编号
                if (mapCarId.get(getLeaningInList.车型) != null && !"".equals(mapCarId.get(getLeaningInList.车型))) {
                    lsh导入名单Model.set车型编号(Integer.parseInt(mapCarId.get(getLeaningInList.车型)));
                }

                lsh导入名单Model.set是否线索("是");
                //判断导入日期是否为空,并校验导入日期是否是在2个月以内，2月以内 是否有效线索 = 是，2月以外 是否有效线索 = 否
                if (getLeaningInList.导入日期 != null && !"".equals(getLeaningInList.导入日期)) {
                    lsh导入名单Model.set导入日期(new java.sql.Date(sdf.parse(getLeaningInList.导入日期).getTime()));
                    lsh导入名单Model.set线索创建日期(new java.sql.Date(sdf.parse(getLeaningInList.导入日期).getTime()));

                    calendar.setTime(sdf.parse(getLeaningInList.导入日期));
                    calendar.add(Calendar.MONTH, 2);
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    if (new Date().before(calendar.getTime())) {
                        lsh导入名单Model.set是否有效线索("是");
                    } else {
                        lsh导入名单Model.set是否有效线索("否");
                    }
                } else {
                    message.append("电话号码：" + getLeaningInList.电话号码 + ",导入日期为空;\n");
                    continue;
                }
                lsh导入名单Model.set备注(getLeaningInList.备注);
                lsh导入名单Model.set更新时间(new Timestamp(new Date().getTime()));
                lsh导入名单Model.set更新账号("i-Log");
                lsh导入名单Model.setInfoSource("iLog");
                lsh导入名单Models.add(lsh导入名单Model);
            }
            lsh导入名单Models = lsh导入名单Repository.save(lsh导入名单Models);
            resultData.setResult("true");
            if (lsh导入名单Models != null) {
                resultData.setMessage("导入名单 ---- 成功导入" + lsh导入名单Models.size() + "条;\n" + message.toString());
            } else {
                resultData.setMessage(message.toString());
            }
        }
        return resultData;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultData dataMoveCusRecordPoolOverWrite(String permit, List<Map> list) throws Exception {
        ResultData resultData = new ResultData("false", null, null);
        StringBuffer message = new StringBuffer();
        if (list != null && !list.isEmpty()) {
            List<顾客记录汇总Model> 顾客记录汇总Models = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

            //获取所有对照车型编号
            List listCarIds = customerInfoRepository.getCarId();
            Map<String, String> mapCarId = getCarIdMap(listCarIds);

            //获取销售顾问账号
            List listSc = customerInfoRepository.getSCInfo(permit);
            Map<String, String> mapSc = getSCInfo(listSc);

            //获取该许可下所有顾客信息
            List listCus = customerInfoRepository.getCusInfo(permit);
            Map<String, String> mapCus = getLeadingInMap(listCus);

            Map<String, String> mapTel = new HashMap<>();
            Map<String, String> mapCrosstown = new HashMap<>();
            Map<String, String> mapOrder = new HashMap<>();

            List<Lsh导入名单Model> lsh导入名单Models = new ArrayList<>();

            for (Map mapBean : list) {
                Object o = BeanUtils.mapToObject(mapBean, GetCusRecordPool.class);
                GetCusRecordPool getCusRecordPool = (GetCusRecordPool) o;
                顾客记录汇总Model model = new 顾客记录汇总Model();
                model.set许可(permit);
                model.set顾客姓名(getCusRecordPool.顾客姓名);
                //校验电话号码
                if (getCusRecordPool.电话号码 == null || "".equals(getCusRecordPool.电话号码)) {
                    message.append("客户姓名：" + getCusRecordPool.顾客姓名 + "，电话号码为空；\n");
                    continue;
                } else {
                    if (mapTel.get(getCusRecordPool.电话号码) != null && !"".equals(mapTel.get(getCusRecordPool.电话号码))) {
                        message.append("客户姓名：" + getCusRecordPool.顾客姓名 + "，电话号码:" + getCusRecordPool.电话号码 +",有重复电话号码；\n");
                        continue;
                    }
                    mapTel.put(getCusRecordPool.电话号码,getCusRecordPool.电话号码);
                    if (mapCus.get(getCusRecordPool.电话号码) != null && !"".equals(mapCus.get(getCusRecordPool.电话号码))) {
                        message.append("客户姓名：" + getCusRecordPool.顾客姓名 + "，电话号码:" + getCusRecordPool.电话号码 + "，用户信息已存在；\n");
                        continue;
                    }
                    model.set电话号码(getCusRecordPool.电话号码);
                }

                //校验销售顾问
                if (getCusRecordPool.销售顾问 == null || "".equals(getCusRecordPool.销售顾问)) {
                    message.append("客户姓名：" + getCusRecordPool.顾客姓名 + "，电话号码:" + getCusRecordPool.电话号码 + ",销售顾问为空;\n");
                    continue;
                } else {
                    if (mapSc.get(getCusRecordPool.销售顾问) == null || "".equals(mapSc.get(getCusRecordPool.销售顾问))) {
                        message.append("未找到销售顾问：" + getCusRecordPool.销售顾问 + ";\n");
                        continue;
                    } else {
                        model.set所属销售顾问(mapSc.get(getCusRecordPool.销售顾问));
                        model.set销售顾问(mapSc.get(getCusRecordPool.销售顾问));
                    }
                }
                if ("先生".equals(getCusRecordPool.顾客性别) || "男".equals(getCusRecordPool.顾客性别)) {
                    model.set顾客性别("男");
                } else if ("女士".equals(getCusRecordPool.顾客性别) || "女".equals(getCusRecordPool.顾客性别)) {
                    model.set顾客性别("女");
                }

                if (getCusRecordPool.留档日期 != null && !"".equals(getCusRecordPool.留档日期)) {
                    model.set留档日期(new java.sql.Date(sdf.parse(getCusRecordPool.留档日期).getTime()));
                }
                //通过ilog_car获取对应车型的车型编号
                if (mapCarId.get(getCusRecordPool.意向车型) != null && !"".equals(mapCarId.get(getCusRecordPool.意向车型))) {
                    model.set意向车型A(Integer.parseInt(mapCarId.get(getCusRecordPool.意向车型)));
                }
                model.set顾客首次渠道(getCusRecordPool.顾客来源);
                model.set顾客渠道(getCusRecordPool.顾客来源);
                model.set顾客来源(getCusRecordPool.顾客渠道);
                model.set保有品牌(getCusRecordPool.保有车品牌);
                if ("是".equals(getCusRecordPool.试乘试驾)) {
                    model.set是否试驾("是");
                } else if ("否".equals(getCusRecordPool.试乘试驾)) {
                    model.set是否试驾("否");
                }
                if ("是".equals(getCusRecordPool.报价)) {
                    model.set是否报价("是");
                } else if ("否".equals(getCusRecordPool.报价)) {
                    model.set是否报价("否");
                }
                if ("是".equals(getCusRecordPool.订单)) {
                    model.set是否订单("是");
                } else if ("否".equals(getCusRecordPool.订单)) {
                    model.set是否订单("否");
                }
                if ("是".equals(getCusRecordPool.交车)) {
                    model.set是否交车("是");
                } else if ("否".equals(getCusRecordPool.交车)) {
                    model.set是否交车("否");
                }
                //顾客记录汇总表的 原顾客级别 是枚举类型;
                //A:一周内订单、B:一个月内订单、C:三个月内订单、D:六个月内订单、E:六个月以上订单、无评级:已订单或交车
                // ilog中新数据和老数据的 原顾客级别 不同，所以需校验 ilog 客户级别 的值，进一步进行数据存储
                if (getCusRecordPool.原顾客级别 != null) {
                    if ("A级客户".equals(getCusRecordPool.原顾客级别) || "A:一周内订单".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("A:一周内订单");
                    } else if ("B级客户".equals(getCusRecordPool.原顾客级别) || "B:一个月内订单".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("B:一个月内订单");
                    } else if ("C级客户".equals(getCusRecordPool.原顾客级别) || "C:三个月内订单".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("C:三个月内订单");
                    } else if ("D级客户".equals(getCusRecordPool.原顾客级别) || "D:六个月内订单".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("D:六个月内订单");
                    } else if ("E:六个月以上订单".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("E:六个月以上订单");
                    } else if ("无评级:已经订单或交车".equals(getCusRecordPool.原顾客级别) || "无评级:已订单或交车".equals(getCusRecordPool.原顾客级别)) {
                        model.set原顾客级别("无评级:已订单或交车");
                    }
                }
                //顾客记录汇总表的 新顾客级别 是枚举类型;
                //A:一周内订单、B:一个月内订单、C:三个月内订单、D:六个月内订单、E:六个月以上订单、无评级:已订单或交车
                // ilog中新数据和老数据的 新顾客级别 不同，所以需校验 ilog 客户级别 的值，进一步进行数据存储
                if (getCusRecordPool.新顾客级别 != null) {
                    if ("A级客户".equals(getCusRecordPool.新顾客级别) || "A:一周内订单".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("A:一周内订单");
                    } else if ("B级客户".equals(getCusRecordPool.新顾客级别) || "B:一个月内订单".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("B:一个月内订单");
                    } else if ("C级客户".equals(getCusRecordPool.新顾客级别) || "C:三个月内订单".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("C:三个月内订单");
                    } else if ("D级客户".equals(getCusRecordPool.新顾客级别) || "D:六个月内订单".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("D:六个月内订单");
                    } else if ("E:六个月以上订单".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("E:六个月以上订单");
                    } else if ("无评级:已经订单或交车".equals(getCusRecordPool.新顾客级别) || "无评级:已订单或交车".equals(getCusRecordPool.新顾客级别)) {
                        model.set新顾客级别("无评级:已订单或交车");
                    }
                }
                if (getCusRecordPool.上次进店日期 != null && !"".equals(getCusRecordPool.上次进店日期)) {
                    model.set上次进店日期(new java.sql.Date(sdf.parse(getCusRecordPool.上次进店日期).getTime()));
                }

                //顾客记录汇总表的 顾客状态 是枚举类型;
                //活跃、休眠、本月、战败、已订单、已交车
                // ilog中新数据和老数据的 新顾客级别 不同，所以需校验 ilog 客户级别 的值，进一步进行数据存储
                if ("成交".equals(getCusRecordPool.顾客状态) || "已交车".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("已交车");
                    mapOrder.put(getCusRecordPool.电话号码, getCusRecordPool.订单日期);
                    mapCrosstown.put(getCusRecordPool.电话号码, getCusRecordPool.成交日期);
                } else if ("活跃".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("活跃");
                } else if ("休眠".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("休眠");
                } else if ("本月".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("本月");
                } else if ("战败".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("战败");
                } else if ("已订单".equals(getCusRecordPool.顾客状态)) {
                    model.set顾客状态("已订单");
                }

                model.set更新时间(new Timestamp(new Date().getTime()));
                model.set更新账号("iLog");
                model.setInfoSource("iLog");
                //新车销售状态对应关系, 如果"是否报价"为否, 是"到店接待"， "是否报价"为是, 是"报价"，"是否订单"为是,是"订金", "是否交车"为是,是"交车"
                if ("是".equals(getCusRecordPool.交车)) {
                    model.set新车销售状态(18);
                } else if ("是".equals(getCusRecordPool.订单)) {
                    model.set新车销售状态(13);
                } else if ("是".equals(getCusRecordPool.报价)) {
                    model.set新车销售状态(10);
                } else {
                    model.set新车销售状态(9);
                }
                顾客记录汇总Models.add(model);

                //校验导入名单表中是否已存在 该线索 ，如果存在，修改导入名单表中记录 的所属销售顾问 问当先 所属销售顾问
                List<Lsh导入名单Model> lsh导入名单ModelList = lsh导入名单Repository.findLatestRecord(model.get电话号码(), permit);
                if (lsh导入名单ModelList != null) {
                    for (Lsh导入名单Model lsh导入名单Model: lsh导入名单ModelList) {
                        lsh导入名单Model.set所属销售顾问(model.get所属销售顾问());
                        lsh导入名单Models.add(lsh导入名单Model);
                    }
                }
            }
            顾客记录汇总Models = repository顾客记录汇总.save(顾客记录汇总Models);
            List<顾客订单记录Model> 顾客订单记录Models = new ArrayList<>();
            if (顾客记录汇总Models != null && !顾客记录汇总Models.isEmpty()) {
                for (顾客记录汇总Model 顾客记录汇总Model : 顾客记录汇总Models) {
                    顾客订单记录Model model = new 顾客订单记录Model();
                    if ("已交车".equals(顾客记录汇总Model.get顾客状态())) {
                        model.set状态("已交车");
                    } else if ("已订单".equals(顾客记录汇总Model.get顾客状态())) {
                        model.set状态("已订单");
                    } else {
                        continue;
                    }
                    model.set许可(顾客记录汇总Model.get许可());
                    model.set顾客编号(顾客记录汇总Model.get顾客编号());
                    model.set车型(顾客记录汇总Model.get意向车型A());
                    model.set销售顾问(顾客记录汇总Model.get所属销售顾问());
                    if (!StringUtils.isNullOrBlank(mapOrder.get(顾客记录汇总Model.get电话号码()))) {
                        model.set订单日期(new java.sql.Date(sdf.parse(mapOrder.get(顾客记录汇总Model.get电话号码())).getTime()));
                    }
                    if (!StringUtils.isNullOrBlank(mapCrosstown.get(顾客记录汇总Model.get电话号码()))) {
                        model.set交车日期(new java.sql.Date(sdf.parse(mapCrosstown.get(顾客记录汇总Model.get电话号码())).getTime()));
                    }

                    model.set渠道(顾客记录汇总Model.get顾客渠道());
                    model.set库存编号(0);
                    model.set更新账号("iLog");
                    model.set更新时间(new Timestamp(new Date().getTime()));
                    model.setInfoSource("iLog");
                    顾客订单记录Models.add(model);
                }
                顾客订单记录Models = repository顾客订单记录.save(顾客订单记录Models);
            }
            //保存已修改的导入名单表信息
            lsh导入名单Repository.save(lsh导入名单Models);
            resultData.setResult("true");
            if (顾客记录汇总Models != null) {
                resultData.setMessage("顾客记录汇总 ---- 成功导入" + 顾客记录汇总Models.size() + "条;\n" + message.toString());
            } else {
                resultData.setMessage(message.toString());
            }
        }
        return resultData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData dataMoveCusIntoTheStoreAndTestDriveOverWrite(String permit, List<Map> list) throws Exception {
        ResultData resultData = new ResultData("false", null, null);
        if (list != null && !list.isEmpty()) {
            String receptionDate = null;
            StringBuffer message = new StringBuffer();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");

            //获取所有对照车型编号
            List listCarIds = customerInfoRepository.getCarId();
            Map<String, String> mapCarId = getCarIdMap(listCarIds);

            //获取销售顾问账号
            List listSc = customerInfoRepository.getSCInfo(permit);
            Map<String, String> mapSc = getSCInfo(listSc);

            //获取该许可下所有顾客信息
            List listCus = customerInfoRepository.getCusInfo(permit);
            Map<String, String> mapCus = getLeadingInMap(listCus);

            Map<String, String> mapTel = new HashMap<>();

            int index = 0;
            for (Map mapBean : list) {
                Object o = BeanUtils.mapToObject(mapBean, GetCusIntoTheStoreAndTestDrive.class);
                GetCusIntoTheStoreAndTestDrive getCusIntoTheStoreAndTestDrive = (GetCusIntoTheStoreAndTestDrive) o;
                顾客进店记录表Model 顾客进店记录表Model = new 顾客进店记录表Model();
                //顾客试驾记录Model 顾客试驾记录Model = new 顾客试驾记录Model();



                顾客进店记录表Model.set许可(permit);
                //校验电话号码
                String tel = getCusIntoTheStoreAndTestDrive.电话号码;
                String telStr = null;
                if (tel == null || "".equals(tel)) {
                    message.append("客户姓名：" + getCusIntoTheStoreAndTestDrive.顾客姓名 + "，电话号码为空；\n");
                    continue;
                } else {
                    BigDecimal bd = new BigDecimal(tel.trim());
                    telStr = bd.toPlainString();
                    if (mapCus.get(telStr) != null && !"".equals(mapCus.get(telStr))) {
                        顾客进店记录表Model.set顾客编号(Integer.parseInt(mapCus.get(telStr)));
                    } else {
                        message.append("客户姓名：" + getCusIntoTheStoreAndTestDrive.顾客姓名 + "，电话号码:" + telStr + ",未找到相关顾客信息；\n");
                        continue;
                    }
                }

                //校验销售顾问
                if (getCusIntoTheStoreAndTestDrive.销售顾问 == null || "".equals(getCusIntoTheStoreAndTestDrive.销售顾问)) {
                    message.append("客户姓名：" + getCusIntoTheStoreAndTestDrive.顾客姓名 + "，电话号码:" + telStr + ",销售顾问为空;\n");
                    continue;
                } else {
                    if (mapSc.get(getCusIntoTheStoreAndTestDrive.销售顾问) == null || "".equals(mapSc.get(getCusIntoTheStoreAndTestDrive.销售顾问))) {
                        message.append("未找到销售顾问：" + getCusIntoTheStoreAndTestDrive.销售顾问 + ";\n");
                        continue;
                    } else {
                        顾客进店记录表Model.set销售顾问(mapSc.get(getCusIntoTheStoreAndTestDrive.销售顾问));
                    }
                }

                if ("是".equals(getCusIntoTheStoreAndTestDrive.是否来电) || "否".equals(getCusIntoTheStoreAndTestDrive.是否来电)) {
                    顾客进店记录表Model.set是否来电(getCusIntoTheStoreAndTestDrive.是否来电);
                }

                if (getCusIntoTheStoreAndTestDrive.电话号码 != null && !"".equals(getCusIntoTheStoreAndTestDrive.电话号码) &&
                        getCusIntoTheStoreAndTestDrive.顾客姓名 != null &&  !"".equals(getCusIntoTheStoreAndTestDrive.顾客姓名)) {
                    顾客进店记录表Model.set是否留档("是");
                } else {
                    顾客进店记录表Model.set是否留档("否");
                }
                顾客进店记录表Model.set是否首次到店(getCusIntoTheStoreAndTestDrive.是否首次到店);
                顾客进店记录表Model.set是否预约(getCusIntoTheStoreAndTestDrive.是否预约);
                顾客进店记录表Model.set是否指定销售顾问(mapSc.get(getCusIntoTheStoreAndTestDrive.是否指定销售顾问));
                if (getCusIntoTheStoreAndTestDrive.进店日期 != null && !"".equals(getCusIntoTheStoreAndTestDrive.进店日期)) {
                    if (!StringUtils.isNullOrBlank(getCusIntoTheStoreAndTestDrive.进店日期)) {
                        receptionDate = getCusIntoTheStoreAndTestDrive.进店日期;
                    }
                    顾客进店记录表Model.set进店日期(new java.sql.Date(sdf1.parse(getCusIntoTheStoreAndTestDrive.进店日期).getTime()));
                }
                if (getCusIntoTheStoreAndTestDrive.到店时间 != null && !"".equals(getCusIntoTheStoreAndTestDrive.到店时间)) {
                    顾客进店记录表Model.set到店时间(new Time(sdfTime.parse(getCusIntoTheStoreAndTestDrive.到店时间).getTime()));
                }
                if (getCusIntoTheStoreAndTestDrive.离店时间 != null && !"".equals(getCusIntoTheStoreAndTestDrive.离店时间)) {
                    顾客进店记录表Model.set离店时间(new Time(sdfTime.parse(getCusIntoTheStoreAndTestDrive.离店时间).getTime()));
                }
                if (getCusIntoTheStoreAndTestDrive.停留时间 != null && !"".equals(getCusIntoTheStoreAndTestDrive.停留时间)) {
                    顾客进店记录表Model.set停留时间(Integer.parseInt(getCusIntoTheStoreAndTestDrive.停留时间.replace("分","")));
                }
                //通过ilog_car获取对应车型的车型编号
                if (mapCarId.get(getCusIntoTheStoreAndTestDrive.偏好车型) != null && !"".equals(mapCarId.get(getCusIntoTheStoreAndTestDrive.偏好车型))) {
                    顾客进店记录表Model.set意向车型A(Integer.parseInt(mapCarId.get(getCusIntoTheStoreAndTestDrive.偏好车型)));
                }
                顾客进店记录表Model.set保有品牌(getCusIntoTheStoreAndTestDrive.现有车型);
                //顾客进店记录表的 是否报价 是枚举类型;
                //A:一周内订单、B:一个月内订单、C:三个月内订单、D:六个月内订单、E:六个月以上订单、无评级:已订单或交车
                // ilog中新数据和老数据的 客户级别 不同，所以需校验 ilog 客户级别 的值，进一步进行数据存储
                if (getCusIntoTheStoreAndTestDrive.客户级别 != null) {
                    if ("A级客户".equals(getCusIntoTheStoreAndTestDrive.客户级别) || "A:一周内订单".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("A:一周内订单");
                    } else if ("B级客户".equals(getCusIntoTheStoreAndTestDrive.客户级别) || "B:一个月内订单".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("B:一个月内订单");
                    } else if ("C级客户".equals(getCusIntoTheStoreAndTestDrive.客户级别) || "C:三个月内订单".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("C:三个月内订单");
                    } else if ("D级客户".equals(getCusIntoTheStoreAndTestDrive.客户级别) || "D:六个月内订单".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("D:六个月内订单");
                    } else if ("E:六个月以上订单".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("E:六个月以上订单");
                    } else if ("无评级:已经订单或交车".equals(getCusIntoTheStoreAndTestDrive.客户级别) || "无评级:已订单或交车".equals(getCusIntoTheStoreAndTestDrive.客户级别)) {
                        顾客进店记录表Model.set客户级别("无评级:已订单或交车");
                    }
                }

                顾客进店记录表Model.set顾客来源(getCusIntoTheStoreAndTestDrive.线索来源);
                顾客进店记录表Model.set信息渠道(getCusIntoTheStoreAndTestDrive.信息渠道);
                //顾客进店记录表的 是否报价 是枚举类型（是，否），所以需校验 ilog 是否报价 的值，进一步进行数据存储
                if ("是".equals(getCusIntoTheStoreAndTestDrive.是否报价)) {
                    顾客进店记录表Model.set是否报价("是");
                } else if ("否".equals(getCusIntoTheStoreAndTestDrive.是否报价)) {
                    顾客进店记录表Model.set是否报价("否");
                }
                //顾客进店记录表的 是否订单 是枚举类型（是，否），所以需校验 ilog 是否订单 的值，进一步进行数据存储
                if ("是".equals(getCusIntoTheStoreAndTestDrive.是否订单)) {
                    顾客进店记录表Model.set是否订单("是");
                } else if ("否".equals(getCusIntoTheStoreAndTestDrive.是否订单)) {
                    顾客进店记录表Model.set是否订单("否");
                }
                //顾客进店记录表的 是否交车 是枚举类型（是，否），所以需校验 ilog 是否交车 的值，进一步进行数据存储
                if ("是".equals(getCusIntoTheStoreAndTestDrive.是否交车)) {
                    顾客进店记录表Model.set是否交车("是");
                } else if ("否".equals(getCusIntoTheStoreAndTestDrive.是否交车)) {
                    顾客进店记录表Model.set是否交车("否");
                }

                顾客进店记录表Model.set到店目的(getCusIntoTheStoreAndTestDrive.到店目的);
                顾客进店记录表Model.set主动集客活动备注(getCusIntoTheStoreAndTestDrive.主动集客活动备注);
                顾客进店记录表Model.set更新时间(new Timestamp(new Date().getTime()));
                顾客进店记录表Model.set更新账号("i-Log");
                顾客进店记录表Model.setInfoSource("iLog");

                顾客进店记录表Model model = repository顾客进店记录表.save(顾客进店记录表Model);
                if (model != null) {
                    顾客记录汇总Model 顾客记录汇总Model = repository顾客记录汇总.findCusByPhoneAndPermit(telStr, permit);
                    if (顾客记录汇总Model != null) {
                        顾客记录汇总Model.set上次进店编号(model.get进店编号());
                    }
                    //通过ilog_car获取对应车型的车型编号
                    if (mapCarId.get(getCusIntoTheStoreAndTestDrive.试驾车型) != null && !"".equals(mapCarId.get(getCusIntoTheStoreAndTestDrive.试驾车型))) {

                        model.set是否试驾("是");
                        repository顾客进店记录表.save(model);

                        String[] startTime = null, endTime = null, testDriveTime = null;
                        if (getCusIntoTheStoreAndTestDrive.试驾开始时间 != null && !"".equals(getCusIntoTheStoreAndTestDrive.试驾开始时间)) {
                            startTime = getCusIntoTheStoreAndTestDrive.试驾开始时间.split(";");
                        }
                        if (getCusIntoTheStoreAndTestDrive.试驾结束时间 != null && !"".equals(getCusIntoTheStoreAndTestDrive.试驾结束时间)) {
                            endTime = getCusIntoTheStoreAndTestDrive.试驾结束时间.split(";");
                        }
                        if (getCusIntoTheStoreAndTestDrive.试驾时长 != null && !"".equals(getCusIntoTheStoreAndTestDrive.试驾时长)) {
                            testDriveTime = getCusIntoTheStoreAndTestDrive.试驾时长.split(";");
                        }
                        if (testDriveTime == null || startTime == null || endTime == null) {
                            continue;
                        }
                        for (int i = 0; i < testDriveTime.length; i++) {
                            顾客试驾记录Model 顾客试驾记录Model = new 顾客试驾记录Model();
                            顾客试驾记录Model.set进店编号(model.get进店编号());
                            顾客试驾记录Model.set试驾车型编号(Integer.parseInt(mapCarId.get(getCusIntoTheStoreAndTestDrive.试驾车型)));
                            顾客试驾记录Model.set试驾开始时间(new Time(sdfTime.parse(startTime.length > i ? startTime[i] : startTime[0]).getTime()));
                            顾客试驾记录Model.set试驾结束时间(new Time(sdfTime.parse(endTime.length > i ? endTime[i] : endTime[0]).getTime()));
                            顾客试驾记录Model.set试驾时长(Integer.parseInt(testDriveTime[i].replace("分","")));
                            顾客试驾记录Model.set更新时间(new Timestamp(new Date().getTime()));
                            顾客试驾记录Model.set更新账号("i-Log");
                            顾客试驾记录Model.setInfoSource("iLog");
                            顾客试驾记录Repository.save(顾客试驾记录Model);

                        }
                    }
                }
                index++;
            }
            resultData.setResult("true");
            if (!StringUtils.isNullOrBlank(receptionDate)) {
                Date aa = DateFormatUtils.getFormatDate(receptionDate, "yyyy-MM-dd");
                receptionDate = DateFormatUtils.getFormatDateToString(aa, "yyyy年MM月");
            } else {
                receptionDate = "";
            }
            resultData.setMessage(receptionDate + "  接待记录 ---- 成功导入" + index + "条;\n" + message.toString());
        }
        return resultData;
    }

    @Override
    public ResultData getILogTransferState(@PathVariable("permit") String permit) {
        ResultData resultData = new ResultData("false", null, null);
        List<TIlogTransferLogModel> list = tIlogTransferLogRepository.findILogTransferModel(permit);
        if (list != null && !list.isEmpty()) {
            for (TIlogTransferLogModel t : list) {
                if (t.getIlogTransferState() == 1) {
                    resultData.setResult("false");
                } else {
                    resultData.setResult("true");
                }
            }
        } else {
            resultData.setResult("true");
        }
        return resultData;
    }

    @Override
    public ResultData saveILogTransferState(String permit, String state) {
        ResultData resultData = new ResultData("false", null, null);
        if (!Strings.isNullOrEmpty(state) && ("0".equals(state) || "1".equals(state))) {
            List<TIlogTransferLogModel> list = tIlogTransferLogRepository.findILogTransferModel(permit);
            TIlogTransferLogModel t = null;
            if (list != null && !list.isEmpty()) {
                t = list.get(0);
                t.setIlogTransferState(Integer.parseInt(state));
                t.setUpdateTime(new Timestamp(new Date().getTime()));
                tIlogTransferLogRepository.save(t);
            } else {
                t = new TIlogTransferLogModel();
                t.setPermit(permit);
                t.setIlogTransferState(Integer.parseInt(state));
                t.setUpdateTime(new Timestamp(new Date().getTime()));
                tIlogTransferLogRepository.save(t);
            }
            resultData.setResult("true");
        }
        return resultData;
    }

    private Map<String, String> getCarIdMap(List list) {
        Map<String, String> resultValues = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map map = (Map) o;
                String type = map.get("type") == null ? "" : map.get("type").toString();
                String lshCarId = map.get("lsh_car_id") == null ? null : map.get("lsh_car_id").toString();
                resultValues.put(type, lshCarId);
            }
        }
        return resultValues;
    }

    private Map<String, String> getLeadingInMap(List list) {
        Map<String, String> resultValues = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map map = (Map) o;
                String tel = map.get("电话号码") == null ? "" : map.get("电话号码").toString();
                String cusId = map.get("顾客编号") == null ? "" : map.get("顾客编号").toString();
                resultValues.put(tel, cusId);
            }
        }
        return resultValues;
    }

    private Map<String, String> getSCInfo(List list) {
        Map<String, String> resultValues = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map map = (Map) o;
                String userName = map.get("人员姓名") == null ? "" : map.get("人员姓名").toString();
                String user = map.get("用户名") == null ? "" : map.get("用户名").toString();
                resultValues.put(userName, user);
            }
        }
        return resultValues;
    }

    public ResultData getScMateResult(List<Map<String, String>> list, String permit) {
        ResultData resultData = new ResultData("false", null, null);
        List values = new ArrayList();
        if (list != null && !list.isEmpty()) {
            for (Map<String, String> map : list) {
                if (map != null) {
                    List queryList = iLogInfoRepository.getScInfoByScName(permit, map.get("scName"));
                    map.put("scEmail", "");
                    if (queryList != null && !queryList.isEmpty()) {
                        Map queryMap = (Map) queryList.get(0);
                        String scEmail = queryMap.get("用户名") == null ? null : queryMap.get("用户名").toString();
                        map.put("scEmail", scEmail);
                    }
                    values.add(map);
                }
            }
        }
        resultData.setResult("true");
        resultData.setPageData(new PageData(values, 1));
        return resultData;
    }

    @Override
    public List getCustomers(customersRequest cust) {
        return apiForLiveRepository.getCustomers(cust);
    }
}
